<?php
	include 'newlib.php';
	include 'newlib_gen.php';
	$page = $_GET["page"];
	$nextpage = "";
	$command = $_REQUEST["command"];
	
	switch ($command) {
		case 'preview':
			previewPage();
			break;
		
		default:
			runPage();
			break;
	}

	function previewPage() {
		global $tmpConnection;
		$pageData = new stdClass();
		$pageData->titel = $_REQUEST['titel'];
		$pageData->pauze = 18;
		$pageData->inhoud = replaceLineEndings($_REQUEST['inhoud']);
		$pageData->bgImage = "";
		$pageData->command = "preview";

		$bgnr = $_REQUEST['imgnr'];
		$special = $_REQUEST['special'];
		$thispage = $_REQUEST['thispage'];
		$theInc = "";
		if ($special > 0) {
			foreach ($tmpConnection->query('select include from special where id=' . $special) as $row) {
				$theInc = $row['include'];
			}
		}
		$rows = $tmpConnection->query('select image from images where id=' . $bgnr);
		foreach ($rows as $row) {
			$pageData->bgImage = $row['image'];
		}
		if ($special == 0) {
			showpage($pageData);
		} else {
			header("Location: special/" . $theInc . "&preview=yes&thispage=" . $thispage . makeParam($pageData->inhoud));
		}

	}

	function runPage() {
		global $tmpConnection;
		$pageData = new stdClass();
		$pageData->pauze = 18;
		$pageData->page = $_REQUEST['page'];
		$stm = $tmpConnection->prepare('select * from live where id = (select max(id) from live)');
		$stm->execute();
		$rs = $stm->fetch(PDO::FETCH_ASSOC);
		$theArray = makeFollowUpArray(explode(";", $rs['volgorde']));
		if (!array_key_exists($pageData->page, $theArray)) {
			$pageData->page = end($theArray);
		}
		$pageData->nextpage = $theArray[$pageData->page];

		$stm2 = $tmpConnection->prepare('select * from vwcontent where id = :page');
		$stm2->execute(array(':page' => $pageData->page));
		$vw = $stm2->fetch(PDO::FETCH_ASSOC);

		# TODO Check for scheduled page...

		$pageData->titel = $vw['titel'];
		$pageData->inhoud = $vw['inhoud'];
		$background = $vw['background'];
		$special = $vw['special'];

		$stm3 = $tmpConnection->prepare('select dur from content where id = :page');
		$stm3->execute(array(':page' => $pageData->page));

		$rs = $stm3->fetch(PDO::FETCH_ASSOC);
		if (!is_null($rs['dur'])) {
			$pageData->pauze = $rs['dur'];
		}
		$stm4 = $tmpConnection->prepare('select image from images where id = :bgnr');
		$stm4->execute(array(':bgnr' => $background));
		$rs = $stm4->fetch(PDO::FETCH_ASSOC);
		$pageData->bgImage = $rs['image'];
		$pageData->theInc = "";
		if ($special > 0) {
			$stm5 = $tmpConnection->prepare('select include from special where id = :special');
			$stm5->execute(array(':special' => $special));

			$rs = $stm5->fetch(PDO::FETCH_ASSOC);
			
			$pageData->theInc = str_replace("\\", "/", $rs['include']);
		}

#	call register(page, titel)
#	call setNP(page, titel, inhoud, pauze, bgImage, special, theInc)
		if ($special == 0) {
			showpage($pageData);
		} else {
			header("Location: special/" . $pageData->theInc . "&thispage=" . $pageData->page . "&nextpage=" . $pageData->nextpage . makeParam($pageData->inhoud));
		}


/*		
		set rs = tmpConnection.execute(sql)
	volgorde = rs("volgorde")
	theArray = split(volgorde, ";")
	if page = "" then page = theArray(0)

	for t=lbound(theArray) to ubound(theArray)
		if theArray(t) = page then
			if t<ubound(theArray) then
				nextpage = theArray(t+1)
			else
				nextpage = theArray(0)
			end if
		end if
	next
	
	sql = "select * from vwcontent where id=" & page
	
	set rs = tmpConnection.execute(sql)
	
	if rs.eof then
		response.redirect "kabelkrant.asp?command=run"
	end if
	
	if not pageIsScheduled(rs) then
		response.redirect "kabelkrant.asp?command=run&page=" & nextpage
	end if
	
	titel = rs("titel")
	inhoud = rs("inhoud")
	bgnr = rs("background")
	special = rs("special")

	sql = "select dur from content where id=" & page
	rs = tmpConnection.execute(sql)

	dur = rs("dur")
	if not isnull(dur) then pauze = dur
	
	sql = "select image from images where id=" & bgnr
	rs = tmpConnection.execute(sql)
	bgImage = rs("image")
	
	if special > 0 then
		sql = "select include from special where id=" & special
		rs = tmpConnection.execute(sql)
		theInc = replace(rs("include"), "\\", "/")
	end if

	closeDatabase

	call register(page, titel)
	call setNP(page, titel, inhoud, pauze, bgImage, special, theInc)

	if special > 0 then
		params = split(inhoud, "<br>")
		response.redirect("special/" & theInc & "&thispage=" & page & "&nextpage=" & nextpage & makeParam(inhoud))
	else
		showpage
	end if

*/

	}

	function showpage($pageData) {
		ob_start();
		require('views/kabelkrantpage.php');
		ob_end_flush();

	}
?>
